package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.util.jwt.AESUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author wjj
 * @date 2023-01-09 20:41
 * @describe
 */
@Service
@Transactional
public class ILoginServiceImpl implements ILoginService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserConverterMapper userConverterMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;


    /**
     * 用户登录功能
     *
     * @param request
     * @return
     */
    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        UserLoginResponse userLoginResponse = new UserLoginResponse();

        Example example = new Example(Member.class);

        //家密码加密后才能进行校验
        AESUtil aesUtil = new AESUtil(request.getUserPwd());
        String pwd = aesUtil.encrypt();

        example.createCriteria().andEqualTo("username", request.getUserName())
                .andEqualTo("password", pwd);

        List<Member> memberList = memberMapper.selectByExample(example);
        Example example1 = new Example(Member.class);
        example1.createCriteria().andEqualTo("username", request.getUserName());
        List<UserVerify> userVerifyList = userVerifyMapper.selectByExample(example1);

        if (memberList.size() == 0) {
            return null;
        }
        for (Member member : memberList) {
            member.setPassword(request.getUserPwd());
            for (UserVerify userVerify : userVerifyList) {
                userLoginResponse = userConverterMapper.converter(member);
                if (member.getIsVerified().equals("N")) {
                    userLoginResponse.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
                    userLoginResponse.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
                    return userLoginResponse;
                } else if (userVerify.getIsExpire().equals("Y")) {
                    userLoginResponse.setCode(UserRetCode.USER_ISEXPIRE_ERROR.getCode());
                    userLoginResponse.setMsg(UserRetCode.USER_ISEXPIRE_ERROR.getMessage());
                    return userLoginResponse;
                } else {
                    userLoginResponse.setCode(UserRetCode.SUCCESS.getCode());
                    userLoginResponse.setMsg(UserRetCode.SUCCESS.getMessage());

                    //将登录用户信息转化成json字符串,并转化为token加密
                    String jsonStr = JSON.toJSONString(member);
                    JwtTokenUtils jwtTokenUtils = JwtTokenUtils.builder().msg(jsonStr).build();
                    String access_token = jwtTokenUtils.creatJwtToken();
                    userLoginResponse.setToken(access_token);
                }
            }
        }
        return userLoginResponse;
    }
}